package tagdb.ui.stats;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.data.statistics.HistogramDataset;
import org.jfree.data.statistics.HistogramType;
import tagdb.algorithm.match.PeptideSpectrumMatch;
import tagdb.model.Result;
import tagdb.model.TagSearchResult;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <b>TagSearchScoreHistogram</b>
 * <p>Class to plot a histogram of the percentage of matched ions over all the spectra.</p>
 *
 * @author Thilo Muth
 */
public class TagSearchScoreHistogram extends Chart {
    private double[] targetData;
    private double[] decoyData;
    private String filename;
    private boolean decoy;

    /**
     * Constructs a denovo score histogram
     *
     * @param resultData
     */
    public TagSearchScoreHistogram(Result resultData) {
        super(resultData);
    }

    @Override
    protected void process(Result resultData) throws IOException {
        // List of all the scores
        List<Double> targetScores = new ArrayList<Double>();
        List<Double> decoyScores = new ArrayList<Double>();
        List<PeptideSpectrumMatch> psms = ((TagSearchResult) resultData).getPeptideSpectrumMatches();
        for (PeptideSpectrumMatch psm : psms) {
            if(psm.isDecoy()) {
                this.decoy = true;
                decoyScores.add(psm.getMatchedIonsRatio());

            } else {

                targetScores.add(psm.getMatchedIonsRatio());
            }
         }

        // Set data.
        targetData = new double[targetScores.size()];
        decoyData = new double[decoyScores.size()];

        //TODO: Use conventional casting method here!
        for (int i = 0; i < targetScores.size(); i++) {
            targetData[i] = targetScores.get(i);
        }

        for (int i = 0; i < decoyScores.size(); i++) {
            decoyData[i] = decoyScores.get(i);
        }

        // Set filename.
        filename = resultData.getFilename();
        setChart();
    }

    @Override
    protected void setChart() {
        HistogramDataset dataset = new HistogramDataset();
        dataset.setType(HistogramType.FREQUENCY);
        dataset.addSeries("Target", targetData, 100);

        if(decoy) dataset.addSeries("Decoy", decoyData, 100);
        chart = ChartFactory.createHistogram(getChartTitle(),
                "Score",
                "Frequency",
                dataset,
                PlotOrientation.VERTICAL,
                true,
                true,
                false);

        XYPlot plot = (XYPlot) chart.getPlot();
        plot.setBackgroundAlpha(0f);
        plot.setDomainGridlinesVisible(false);
        plot.setRangeGridlinesVisible(false);
        plot.setOutlineVisible(false);

        XYBarRenderer renderer = new XYBarRenderer();
        renderer.setShadowVisible(false);
        plot.setRenderer(renderer);
    }

    @Override
    public String getChartTitle() {
        return "Tag Search Score Histogram";
    }
}
